package com.modouyu.modules.crawler.spider.listener;


import com.modouyu.common.utils.RedisKeys;
import com.modouyu.common.utils.RedisUtils;
import com.modouyu.modules.crawler.spider.CrawlerLogProcessor;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.SpiderListener;

import java.util.regex.Pattern;

/**
 * ${DESCRIPTION}
 *
 * @author yangcb
 * @email mudouyu@aliyun.com
 * @create 2018-02-01 11:35
 **/
public class NovelSpiderListener implements SpiderListener {


    private RedisUtils redisUtils;
    private String type;


    public NovelSpiderListener(RedisUtils redisUtils, String type) {
        this.redisUtils = redisUtils;
        this.type = type;
    }


    private static final Logger LOGGER = LoggerFactory.getLogger(NovelSpiderListener.class);


    @Override
    public void onSuccess(Request request) {

    }

    @Override
    public void onError(Request request) {

        int statusCode = Integer.parseInt(request.getExtra("statusCode").toString());
        if (HttpStatus.SC_NOT_FOUND == statusCode) {
            String url = request.getUrl();
            //将404页面移除
            if (Pattern.compile(CrawlerLogProcessor.URL_LIST).matcher(url).find()) {
                redisUtils.hmDelete(RedisKeys.getCrawlerLogKey(String.format(CrawlerLogProcessor.PAGE_URL_KEY, type)), url);
            } else if (Pattern.compile(CrawlerLogProcessor.URL_DETAIL).matcher(url).find()) {
                redisUtils.hmDelete(RedisKeys.getCrawlerLogKey(String.format(CrawlerLogProcessor.CHAPTER_URL_KEY, type)), url);
            } else if (Pattern.compile(CrawlerLogProcessor.URL_POST).matcher(url).find()) {
                redisUtils.hmDelete(RedisKeys.getCrawlerLogKey(String.format(CrawlerLogProcessor.ARTICLE_URL_KEY, type)), url);
                redisUtils.hmDelete(RedisKeys.getCrawlerLogKey(String.format(CrawlerLogProcessor.ARTICLE_AUTHOR_KEY, type)), url);
            }
        }
        LOGGER.info("download page error code:{}", statusCode);
    }
}
